/*
 * FeedExpress
 * (C) Copyright 2003 Jeppe Cramon (jeppe@cramon.dk)
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Disclaimer:
 * -----------
 * This software is provided "as is" without warranty of any kind,
 * either expressed or implied. The entire risk as to the
 * quality and performance of the software is with you. Should the
 * software prove defective, you assume the cost of all necessary
 * servicing, repair, or correction. In no event shall the author,
 * copyright holder, or any other party who may redistribute the
 * software be liable to you for damages, including any general,
 * special, incidental, or consequential damages arising out of
 * the use or inability to use the software (including, but not
 * limited to, loss of data, data being rendered inaccurate, loss of
 * business profits, loss of business information, business
 * interruptions, loss sustained by you or third parties, or a
 * failure of the software to operate with any other software) even
 * if the author, copyright holder, or other party has been advised
 * of the possibility of such damages. 
 * 
 */

using System;

namespace Pbdms.BusinessLogic.RSS
{
	/// <summary>
	/// Represents the information about the channel for a subscription
	/// </summary>
	[Serializable]
	public class ChannelInformation {
		private string mTitle;
		private string mLink;
		private string mDescription;
		private string mLanguage;
		private string mCreator;
		private string mRights;
		private DateTime mDate;
		private string mGeneratorAgent;
		private string mErrorReportsTo;
		private UpdatePeriodEnum mUpdatePeriod;
		private int mUpdateFrequency;
		private DateTime mUpdateBase;
		private Subscription mSubscription;
		private string mCSSStyle;


		public event EventHandler UpdateSettingsChanged;

		public ChannelInformation() {
		}
		
		public ChannelInformation(Subscription sub) {
			mSubscription = sub;
		}

		public enum UpdatePeriodEnum { Minute, Hour, Day, Month };
		
		public static UpdatePeriodEnum updatePeriodStringToEnum(string name) {
			switch (name.ToLower()) {
				case "minute":
					return UpdatePeriodEnum.Minute;
				case "hourly":
					return UpdatePeriodEnum.Hour;
				case "daily":
					return UpdatePeriodEnum.Day;
				case "monthly":
					return UpdatePeriodEnum.Month;
				default:
					return UpdatePeriodEnum.Hour;
			}
		}

		public static string updatePeriodEnumToString(UpdatePeriodEnum updatePeriode) {
			switch (updatePeriode) {
				case UpdatePeriodEnum.Minute:
					return "minute";
				case UpdatePeriodEnum.Hour:
					return "hourly";
				case UpdatePeriodEnum.Day:
					return "daily";
				case UpdatePeriodEnum.Month:
					return "monthly";
				default:
					throw new Exception("Unknown UpdatePeriodeEnum value " + updatePeriode);
			}
		}

		public Subscription subscription {
			get { return mSubscription; }
			set { mSubscription = value; }
		}

		public string cssStyle {
			get { return mCSSStyle; }
			set { mCSSStyle = value; }
		}

		/// <summary>
		/// Return the update interval in milliseconds.
		/// </summary>
		/// <returns></returns>
		public int getUpdateInterval() {
			// init with the frequency in seconds
			int interval = updateFrequency*1000;
			if (interval == 0) {
				// Interval == 0 isn't a valid number
				interval = 1000;
			}
			switch (mUpdatePeriod) {
				case UpdatePeriodEnum.Minute:
					return interval*60;
				case UpdatePeriodEnum.Hour:
					return interval*3600;
				case UpdatePeriodEnum.Day:
					return interval*3600*24;
				case UpdatePeriodEnum.Month:
					// 30 days is assumed in a month
					return interval*3600*24*30;
			}
			return 1;
		}

		public string title {
			get { return mTitle; }
			set { mTitle = value; }
		}

		public string link {
			get { return mLink; }
			set { mLink = value; }
		}
		public string description {
			get { return mDescription; }
			set { mDescription = value; }
		}
		public string language {
			get { return mLanguage; }
			set { mLanguage = value; }
		}
		public string creator {
			get { return mCreator; }
			set { mCreator = value; }
		}
		public string rights {
			get { return mRights; }
			set { mRights = value; }
		}
		public string generatorAgent {
			get { return mGeneratorAgent; }
			set { mGeneratorAgent = value; }
		}
		public string errorReportsTo {
			get { return mErrorReportsTo; }
			set { mErrorReportsTo = value; }
		}
		public UpdatePeriodEnum updatePeriod {
			get { return mUpdatePeriod; }
			set { 
				mUpdatePeriod = value; 
				if (UpdateSettingsChanged != null) {
					UpdateSettingsChanged(this, EventArgs.Empty);
				}
			}
		}
		public int updateFrequency {
			get { return mUpdateFrequency; }
			set { 
				mUpdateFrequency = value; 
				if (UpdateSettingsChanged != null) {
					UpdateSettingsChanged(this, EventArgs.Empty);
				}
			}
		}

		public DateTime updateBase {
			get { return mUpdateBase; }
			set { mUpdateBase = value; }
		}
		public DateTime date {
			get { return mDate; }
			set { mDate = value; }
		}

		public override string ToString() {
			string returnValue;
			returnValue = "Title: " + title + "\r\n";
			returnValue += "Link: " + link + "\r\n";
			returnValue += "Description: " + description + "\r\n";
			returnValue += "Language: " + language + "\r\n";
			returnValue += "Creator: " + creator + "\r\n";
			returnValue += "Rights: " + rights + "\r\n";
			returnValue += "Date: " + date + "\r\n";
			returnValue += "GeneratorAgent: " + generatorAgent + "\r\n";
			returnValue += "ErrorReportsTo: " + errorReportsTo + "\r\n";
			returnValue += "UpdatePeriod: " + updatePeriod + "\r\n";
			returnValue += "UpdateFrequency: " + updateFrequency + "\r\n";
			returnValue += "UpdateBase: " + updateBase;
			return returnValue;
		}

		public override bool Equals(object obj) {
			if (obj is ChannelInformation) {
				ChannelInformation other = (ChannelInformation)obj;
				if (other.creator == this.creator &&
					other.date.Equals(this.date) &&
					other.description == this.description &&
					other.errorReportsTo == this.errorReportsTo &&
					other.generatorAgent == this.generatorAgent && 
					other.language == this.language &&
					other.link == this.link &&
					other.rights == this.rights &&
					other.title == this.title &&
					other.updateBase.Equals(this.updateBase) &&
					other.updateFrequency == this.updateFrequency &&
					other.updatePeriod == this.updatePeriod) {
					
					return true;
				}
			}
			return false;
		}

		public override int GetHashCode() {
			int result = 17;
			result = 37*result + this.creator.GetHashCode();
			result = 37*result + this.date.GetHashCode();
			result = 37*result + this.description.GetHashCode();
			result = 37*result + this.errorReportsTo.GetHashCode();
			result = 37*result + this.generatorAgent.GetHashCode();
			result = 37*result + this.language.GetHashCode();
			result = 37*result + this.link.GetHashCode();
			result = 37*result + this.rights.GetHashCode();
			result = 37*result + this.title.GetHashCode();
			result = 37*result + this.updateBase.GetHashCode();
			result = 37*result + this.updateFrequency;
			result = 37*result + this.updatePeriod.GetHashCode();
			return result;
		}
	}

}
